<?
if(!defined('_AOW93KKDIEQ'))die("who are you?");
include ("phpmailer/function.sendmail.php");

CheckTimeOut();

function set_Cookie($name, $value = '',$time=0,$stct=1,$path="") {
	$mainURL="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

	$expire = ($time>0)? $time:(time() + 60 * 60 * 50);
	$secure = (($_SERVER['HTTPS'] == 'on' OR $_SERVER['HTTPS'] == '1') ? true : false);
	$p = parse_url($mainURL);
	if ($stct == 1 && $p['host']!='localhost') {
		
		if($path=="")
		{
			$dir=str_replace("\\","/",dirname($p['path']));
			$path = !empty($dir) ? $dir.'/' : '/';
			if($path=="//")$path="/";
		}
		$domain = $p['host'];
		@setcookie($name, $value, $expire, $path, $domain, $secure);
	}
	else @setcookie($name,$value,$expire);
	$_COOKIE[$name]=$value;
}

function CheckLoginCP()
{
global $dbsql;
	if(isset($_SESSION['userIdCP']) && $_SESSION['userIdCP']>0)
	{
		$rs=$dbsql->query("select * from ".$GLOBALS['first_table']."tbluser where iUserId=".$_SESSION['userIdCP']);
		if($rq=$dbsql->fetch_array($rs))
		{
			$GLOBALS['username']=$rq['cUserName'];
			$GLOBALS['userid']=$rq['iUserId'];
			return;
		}
	}
	header("location: index.php?opt=login");
	exit;
}


function CheckLoginChat()
{
global $dbsql,$act;

	$ssid= isset($_COOKIE[_NAMECOOKIELOGINUSER]) ? $_COOKIE[_NAMECOOKIELOGINUSER] : "";
	$logined=false;
	if(!empty($ssid))
	{
		$rs=$dbsql->query("select * from ".$GLOBALS['first_table']."tbluser where cSId='$ssid' and iStatus=2");
		if($rq=$dbsql->fetch_array($rs))
		{
			$GLOBALS['username']=$rq['cUserName'];
			$GLOBALS['userid']=$rq['iUserId'];
			UpdateTimeOutUser($rq['iUserId']);
			$logined=true;
		}
	}
	if(!$logined)
	{
		if($act=="check")
		{
			echo 'timeout';
			exit;
		}
		header("location: chat.php?opt=login");
		exit;
	}
}

function GetSupportInfo($name)
{
global $dbsql,$hasOnline,$listroom;

	$hasOnline=false;
	$listroom=array();
	
	$sql_select="select iUserId,cWebsite,cBanner from ".$GLOBALS['first_table']."tbluser where cUserName='$name'";
	$rs=$dbsql->query($sql_select);
	if($result=$dbsql->fetch_array($rs))
	{
		$GLOBALS['supportid'] = $result['iUserId'];
		$GLOBALS['website']   = $result['cWebsite'];
		$GLOBALS['Banner']    = $result['cBanner'];
		
		$sql_select="select iUserId,cRoomName,iStatus,cUserName from ".$GLOBALS['first_table']."tbluser where 
			iUserId=".$GLOBALS['supportid']." or iParentId=".$GLOBALS['supportid'];
		$rs=$dbsql->query($sql_select);
		//if($dbsql->num_rows($rs)==1)return;
		while($result=$dbsql->fetch_array($rs))
		{
			$listroom[]=$result;
			if($result['iStatus']==2)$hasOnline=true;
		}
	}
}

function CheckLoginGuest()
{
global $dbsql;

	$ssid= isset($_COOKIE[_NAMECOOKIELOGINGUEST]) ? $_COOKIE[_NAMECOOKIELOGINGUEST] : "";
	if(!empty($ssid))
	{
		$rs=$dbsql->query("select * from ".$GLOBALS['first_table']."tblchat where cSId='$ssid' and iStatus=1");
		if($rq=$dbsql->fetch_array($rs))
		{
			$GLOBALS['roomid']=$rq['iChatId'];
			$GLOBALS['username']=$rq['cRoomName'];
			$GLOBALS['Email']=$rq['cEmail'];
			UpdateTimeOutGuest($rq['iChatId']);
			return true;
		}else
		{
			return false;
		}
	}else
	{
		return false;
	}
}

function CheckAdminOnlineChat($name)
{
global $dbsql;
	$rs=$dbsql->query("select * from ".$GLOBALS['first_table']."tbluser where cUserName='$name'");
	if($rq=$dbsql->fetch_array($rs))
	{
		$GLOBALS['SUPPORT_PARENT']=$rq;
		if($rq['iStatus']==2)
		{			
			return true;
		}
	}
	return false;
}

function CheckTimeOut()
{
global $dbsql;
	$textlogout=">>>>>>>>Logouted >>>>>".date("Y-m-d / H:i:s");
	$dbsql->query("update ".$GLOBALS['first_table']."tblchat set iStatus=0, cText=concat(cText,'".$username."++".$textlogout."$$') where iStatus!=0 and iTimeOut<"._TIME);	
	$dbsql->query("update ".$GLOBALS['first_table']."tbluser set iStatus=3 where iTimeOut<"._TIME);
}

function UpdateTimeOutGuest($id)
{
global $dbsql;
	$timeout=_TIME+_TIMEOUT_;
	$dbsql->query("update ".$GLOBALS['first_table']."tblchat set iTimeOut='".$timeout."' where iChatId='$id'");	
}

function UpdateTimeOutUser($id)
{
global $dbsql,$act;
	$timeout=_TIME+_TIMEOUT_;
	$status="";
	if($act=="check")
	{
		$status=",iStatus=2";
	}
	$dbsql->query("update ".$GLOBALS['first_table']."tbluser set iTimeOut='".$timeout."' ".$status." where iUserId='$id'");	
}

function LoadMsOfUser()
{
global $listguest,$dbsql,$userid,$text;
	$arr=explode("||",$listguest);
	$list=array();
	for($i=0;$i<count($arr);$i++)
	{
		$arr2=explode("|",$arr[$i]);
		$list['id'][$i]=intval($arr2[0]);
		$list['row'][$i]=intval($arr2[1]);
	}

	$sql_select="select * from ".$GLOBALS['first_table']."tblchat where iUserId='$userid' order by iChatId";
	$rs=$dbsql->query($sql_select);

	$data= ".(*_*)";
	while($rq=$dbsql->fetch_array($rs))
	{

		$guest=$rq['cRoomName'];
		$id=$rq['iChatId'];
		$status=$rq['iStatus'];
		$text=$rq['cText'];
		$rowed=0;
		for($i=0;$i<count($list['id']);$i++)
		{
			if($id==$list['id'][$i])
			{
				$rowed=$list['row'][$i];
			}
		}
		$textarr=explode("$$",$text,$rowed+1);
		$text=  $textarr[$rowed];

		$data.= $guest;
		$data.= "||";
		$data.= $id;
		$data.= "||";
		$data.= $status;
		$data.= "||";
		$data.= $text;
		$data.= "||".$rq['cGuestTyping'];
		$data.= "(*_*)";
	}
	return $data;
}



function LoadMsGuest()
{
global $dbsql,$roomid,$rowed,$text;
	$sql_select="select * from ".$GLOBALS['first_table']."tblchat where iChatId='$roomid'";
	$rs=$dbsql->query($sql_select);
	$data= ".(*_*)";
	while($rq=$dbsql->fetch_array($rs))
	{
		$text=$rq['cText'];
		$textarr=explode("$$",$text,$rowed+1);
		$text=$textarr[$rowed];
		$data.= $text;
		$typingtext=$rq['cUserTyping'];
	}
	$data.= "(*_*)".$typingtext;
	return $data;
}

function getBanner()
{
	$path=_DIRBANNER.$GLOBALS['Banner'];
	if(is_file($path))
	{
		return $path;
	}
	return _BANNERDEFAULT;
}

// xu ly up file anh:
function upImage($imagefile,$folder,$widthmax=0,$heightmax=0)
{	
global $errupfile;
	if(!is_dir($folder))@mkdir($folder);
	
	$imageName  = $imagefile["name"];
	$imageName=str_replace(" ","_",$imageName);
	$imageName=str_replace("\\\'","_",$imageName);
	$imageName=str_replace("\\\'","_",$imageName);
	$imageName=str_replace("'","_",$imageName);
	$typeimage  = $imagefile['type'];
	$size       = $imagefile['size'];
	$temp_name  = $imagefile['tmp_name'];
	if(substr($folder,strlen($folder)-1,1)!="/")$folder.="/";
	if(!empty($imageName))
	{
		$arrname=explode(".",$imageName);
		$ename=$arrname[count($arrname)-1];
		$fname=substr($imageName,0,strlen($imageName)-strlen($ename)-1);
		
		$i=1;
		while(file_exists($folder.$imageName))
		{			
			$imageName=$fname."_".$i.".".$ename;
			$i++;
		}
		$typeimage=strtolower($typeimage);
		if (strpos($typeimage,"image/")===false && strpos($typeimage,"application/x-shockwave-flash")===false)
		{
			$errupfile = "Not support images format :".$typeimage;
			return false;
		}		
		
		if(! move_uploaded_file($temp_name,$folder.$imageName))
		{ 		
			$errupfile = "Can not copy file ! !";
			return false;
		}		
		return $imageName;
	}
	return false;
}

function TrMd5($code)
{
	$hash="keka";
	$code=$code.$hash;
	$md5=md5($hash.md5($code));
	return $md5;
}
?>